﻿/**
 * Android 文本框，滚动模块，用来解决移动设备软键盘覆盖文本框的问题
 * use 方法接收一个可以滚动的容器。
 */
define(function (require, exports, module) {
    "require:nomunge,exports:nomunge,module:nomunge";

    var self = exports;
    var $event = require('mokit/event');
    var $ = require('mokit/jquery');
    var commonUtils = require('./utils');

    self.use = function (container, bottomSpace) {
        if (!commonUtils.isAndroid() && !commonUtils.isIOS('7')) return;
        bottomSpace = bottomSpace || 10;
        //创建处理事件
        self.handler = function () {
            var box = $(this);
            container.css('overflow', 'hidden');
            setTimeout(function () {
                var topPosition = (box.offset().top - container.offset().top) + container.prop('scrollTop');
                var bottomPosition = topPosition - container.outerHeight() + box.outerHeight() + bottomSpace;
                //container.prop('scrollTop', bottomPosition);
                container.animate({
                    scrollTop: bottomPosition
                }, 300, function () {
                    container.css('overflow', 'auto');
                    box.focus();
                });
            }, 500);
        };
        var inputs = container.find('input[type=text],input[type=password],input[type=email],input[type=number]');
        //绑定事件
        inputs.on('touchstart', self.handler);
    };
});